package com.auicyh.tylb.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.auicyh.tylb.R;
import com.auicyh.tylb.inter.CustomDialogCallBacks;
import com.auicyh.tylb.utils.LogUtils;
import com.auicyh.tylb.utils.ScreenUtil;
import com.auicyh.tylb.utils.StringUtils;

/**
 * 作者：  Ocean 2018/8/28 9:31
 * 信息：  Oil com.auicyh.tylb.dialog
 * 功能：  基础的对话框 键盘
 * 邮箱：   1126067113@qq.com
 * 修改人：
 * 公司: 北京天元陆兵汽车科技有限公司
 * @author Ocean
 */
public abstract class BaseDialogFragment extends DialogFragment implements View.OnClickListener {
    /** 标题 */
    private String mTitle;
    /** 提示信息 */
    private String mMessage;
    /** 积极的按钮 */
    private String mPositive;
    /** 消极的按钮 */
    private String mNegative;
    /** 标题栏颜色 */
    private int mTitleColor;
    /** 提示信息的位置(默认开始位置) */
    private int mGravity = Gravity.CENTER_VERTICAL;
    /** 是否显示右上角的关闭按钮 */
    private boolean isShowClose = true;
    /** 是否显示消极的按钮(默认不显示) */
    private boolean isNegative = false;
    /** 宽占比 */
    private double mWide = 0.48;
    /** 高占比 */
    private double mHigh = 0;
    /** 是否根据内容(默认不根据) */
    private boolean isHighContent = false;
    /** 接口回调 */
    private CustomDialogCallBacks mCallBacks;

    /** 标题栏 */
    public static final String KEY_DIALOG_TITLE = "key_dialog_title";
    /** 内容 */
    public static final String KEY_DIALOG_MESSAGE = "key_dialog_message";
    /** 积极的按钮 */
    public static final String KEY_DIALOG_POSITIVE = "key_dialog_positive";
    /** 消极的按钮 */
    public static final String KEY_DIALOG_NEGATIVE = "key_dialog_negative";

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (null != dialog) {
            DisplayMetrics dm = new DisplayMetrics();
            Activity activity = getActivity();
            if (null != activity) {
                // 尺寸
                float inch = ScreenUtil.getScreenInch(activity);
                // 宽
                float widthInch = ScreenUtil.getWidthInch(activity);
                // 高
                float heightInch = ScreenUtil.getHeightInch(activity);
                LogUtils.e(true, "尺寸" + inch + "宽" + widthInch + "高" + heightInch);
                activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
                Window window = dialog.getWindow();
                if (null != window) {
                    if (widthInch > heightInch) {
                        // dm.widthPixels 可操作区域的宽
                        float px = (float) (dm.widthPixels * mWide);
                        int dialogWight = ScreenUtil.getDp(activity, px);
                        // 对话框宽大于440dp
                        int maxWightDp = 440;
                        if (dialogWight >= maxWightDp) {
                            px = ScreenUtil.getPx(activity, 440);
                        }
                        LogUtils.e(true, "对话框像素" + px + "原宽" + dialogWight + "dp" + ScreenUtil.getDp(activity, px));
                        if (isHighContent) {
                            window.setLayout((int) px, ViewGroup.LayoutParams.WRAP_CONTENT);
                        } else {
                            // 如果是0赋予默认值0.85
                            mHigh = mHigh == 0 ? 0.85 : mHigh;
                            window.setLayout((int) px, (int) (dm.heightPixels * mHigh));
                        }
                    } else {
                        // ViewGroup.LayoutParams.WRAP_CONTENT
                        if (isHighContent) {
                            window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        } else {
                            // 如果是0赋予默认值0.6
                            mHigh = mHigh == 0 ? 0.65 : mHigh;
                            window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, (int) (dm.heightPixels * mHigh));
                        }
                    }

                }
            }
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Dialog dialog = getDialog();
        // 可取消
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        // 设置无标题
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (null != window) {
            // 设置透明背景
            window.setBackgroundDrawableResource(android.R.color.transparent);
        }
        View view = inflater.inflate(R.layout.dialog_base_layout, container);
        FrameLayout frameLayout = view.findViewById(R.id.tylb_dialog_content_fl);
        // 标题栏
        TextView titleTv = view.findViewById(R.id.tylb_dialog_title_tv);
        if (StringUtils.isStrNull(mTitle)) {
            mTitle = "温馨提示";
        }
        titleTv.setText(mTitle);
        Activity activity = getActivity();
        if (activity != null && mTitleColor != 0) {
            titleTv.setTextColor(ContextCompat.getColor(activity, mTitleColor));
        }
        // 右上角关闭
        ImageButton closeIb = view.findViewById(R.id.tylb_dialog_close_iv);
        closeIb.setOnClickListener(this);
        if (isShowClose) {
            closeIb.setVisibility(View.VISIBLE);
        } else {
            closeIb.setVisibility(View.GONE);
        }
        // 按钮布局
        LinearLayout buttonLl = view.findViewById(R.id.tylb_dialog_bt_ll);
        // 取消按钮
        Button negativeBt = view.findViewById(R.id.tylb_dialog_not_bt);
        if (StringUtils.isStrNull(mNegative)) {
            mNegative = "取消";
        }
        negativeBt.setText(mNegative);
        negativeBt.setOnClickListener(this);
        if (isNegative) {
            negativeBt.setVisibility(View.VISIBLE);
        } else {
            negativeBt.setVisibility(View.GONE);
        }
        // 确定按钮
        Button positiveBt = view.findViewById(R.id.tylb_dialog_ok_bt);
        positiveBt.setOnClickListener(this);
        if (StringUtils.isStrNull(mPositive)) {
            mPositive = "确定";
        }
        positiveBt.setText(mPositive);
        onViewDialog(frameLayout);
        onChildView(buttonLl, titleTv, closeIb, negativeBt, positiveBt);
        return view;
    }

    /**
     * 重写这个方法，添加对话框北京内容
     * @param frameLayout frameLayout
     */
    public abstract void onViewDialog(FrameLayout frameLayout);

    /**
     * 重新这个方法进行一些个性化设置
     * @param titleTv 标题
     * @param closeIb 关闭按钮
     * @param n       消极按钮
     * @param p       积极按钮
     */
    public void onChildView(LinearLayout linearLayout, TextView titleTv, ImageButton closeIb, Button n, Button p) {

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tylb_dialog_close_iv) {
            if (mCallBacks != null) {
                mCallBacks.onClose();
            }
            // commitAllowingStateLoss();
            dismiss();
        } else if (i == R.id.tylb_dialog_not_bt) {
            if (mCallBacks != null) {
                mCallBacks.onNegative();
            }
            dismiss();
        } else if (i == R.id.tylb_dialog_ok_bt) {
            if (mCallBacks != null) {
                mCallBacks.onPositive();
            }
            dismiss();
        }
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }

    public String getMessage() {
        return mMessage;
    }

    public void setMessage(String message) {
        mMessage = message;
    }


    public String getPositive() {
        return mPositive;
    }

    public void setPositive(String positive) {
        mPositive = positive;
    }

    public String getNegative() {
        return mNegative;
    }

    public void setNegative(String negative) {
        mNegative = negative;
    }

    public boolean isShowClose() {
        return isShowClose;
    }

    public void setShowClose(boolean showClose) {
        isShowClose = showClose;
    }

    public boolean isNegative() {
        return isNegative;
    }

    public void setNegative(boolean negative) {
        isNegative = negative;
    }

    public double getWide() {
        return mWide;
    }

    public void setWide(double wide) {
        mWide = wide;
    }

    public double getHigh() {
        return mHigh;
    }

    public void setHigh(double high) {
        mHigh = high;
    }

    public boolean isHighContent() {
        return isHighContent;
    }

    public void setHighContent(boolean highContent) {
        isHighContent = highContent;
    }

    public CustomDialogCallBacks getCallBacks() {
        return mCallBacks;
    }

    public void setCallBacks(CustomDialogCallBacks callBacks) {
        mCallBacks = callBacks;
    }

    public int getGravity() {
        return mGravity;
    }

    public void setGravity(int gravity) {
        mGravity = gravity;
    }

    public int getTitleColor() {
        return mTitleColor;
    }

    public void setTitleColor(int titleColor) {
        mTitleColor = titleColor;
    }
}
